package regominer2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;

import regominer2.Hexagon;
import regominer2.Board;




class MyMouseListener implements java.awt.event.MouseListener
{
	public MyMouseListener() {
    }
	
	public void mouseClicked(java.awt.event.MouseEvent e) {

		System.out.println("click " + e.getX() + ", " + e.getY());
	        if((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK ) > 0 && (e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) > 0)
	        {
	        	System.out.println("BUM");
	        	RegomineR2.board.Bclick(RegomineR2.board.findTileByPos(e.getX(), e.getY()));
	        }
	        if(e.getButton() == MouseEvent.BUTTON1)
	        {
	        	JToggleButton b = (JToggleButton) e.getSource();
	        	Hexagon t = RegomineR2.board.findTileByPos(e.getX(), e.getY());
	        	int value = t.getValue();
	        	if(value == 9)
	        		b.setText("X");
	        	else if(value == 0)
	        	{
	        		for(Hexagon h : t.getNeighbors())
	        		{
	        			
	        		}
	        	}
	        	else
	        		b.setText(String.valueOf(value));
	        	RegomineR2.board.Lclick(t);
	        }
	        if(e.getButton() == MouseEvent.BUTTON3)
	        {
	        	RegomineR2.board.Rclick(RegomineR2.board.findTileByPos(e.getX(), e.getY()));
	        }
	    }
	    
	    public void mouseReleased(MouseEvent e) {
			System.out.println("release");
	        if(((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK ) > 0 && e.getButton() == MouseEvent.BUTTON3)
	        		||
	        		((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK ) > 0 && e.getButton() == MouseEvent.BUTTON1))
	        {
	        	System.out.println("BUM2");
	        }
	    }

		public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("double click");
		}


		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("pressed");
			
		}
}

public class RegomineR2 extends javax.swing.JFrame {
	public static Board board;
	public static int tileSize = 30;
	public static int margin = 0;
	public static MyMouseListener mouseListener;
	public static int size = 11;
	
	
	private LinkedList<JPanel> panel;
	private LinkedList<JToggleButton> buttons;

	public void createGUI()
	{
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		setSize(600, 500);
		panel = new LinkedList();
		for(int i=0; i<size+1; i++)
		{
			int width = size - (i+1)%2;
			JPanel p = new JPanel();
			p.setLayout(new FlowLayout());
			if(i < size)
			{
				for(int j=0; j < width; j++)
				{
					JToggleButton b = new JToggleButton(" ");
					b.addMouseListener(mouseListener);
					p.add(b);
					buttons.add(b);
				}
			}
			this.add(p);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
	}
	
	
	public RegomineR2() {
		super();
		buttons = new LinkedList();
		mouseListener = new MyMouseListener();
		createGUI();
	}	
	
	public static void main(String[] args)
	{
		board = new Board(size);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				RegomineR2 inst = new RegomineR2();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});		
	}
}


class myButton extends JToggleButton
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	myButton(String label)
	{
		super(label);
	}
}

